/*	Author		: Arga
 *	Created On	: Apr 30, 2011
 *	Description	: 
 */

package team8.paacdroid.activity;

import java.util.List;

import team8.paacdroid.R;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class ShowLocationActivity extends RootActivity {
	private TextView latituteField;
	private TextView longitudeField;
	private LocationManager locationManager;
	private String provider;
	Location mLocation;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.show_location_activity);
		latituteField = (TextView) findViewById(R.id.TextView02);
		longitudeField = (TextView) findViewById(R.id.TextView04);

		// Get the location manager
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		// Define the criteria how to select the location provider -> use
		// default
		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);
		// locationManager.requestLocationUpdates(provider, 400, 1, this);

		// Initialize the location fields
		if (location != null) {
			System.out.println("Provider " + provider + " has been selected.");
			int lat = (int) (location.getLatitude());
			int lng = (int) (location.getLongitude());
			latituteField.setText(String.valueOf(lat));
			longitudeField.setText(String.valueOf(lng));
		} else {
			latituteField.setText("Provider not available");
			longitudeField.setText("Provider not available");
		}

		List<String> providers = locationManager.getProviders(true);
		StringBuilder mSB = new StringBuilder("Providers:\n");
		for (int i = 0; i < providers.size(); i++) {
			locationManager.requestLocationUpdates(providers.get(i), 5000,
					2.0f, new LocationListener() {

						// these methods are required
						public void onLocationChanged(Location location) {
						}

						public void onProviderDisabled(String arg0) {
						}

						public void onProviderEnabled(String provider) {
						}

						public void onStatusChanged(String a, int b, Bundle c) {
						}
					});
			mSB.append(providers.get(i)).append(": \n");
			mLocation = locationManager.getLastKnownLocation(providers.get(i));
			if (mLocation != null) {
				mSB.append(mLocation.getLatitude()).append(" , ");
				mSB.append(mLocation.getLongitude()).append("\n");
			} else {
				mSB.append("Location can not be found");
			}
		}
		latituteField.setText(mSB.toString());
	}
}
